package com.example.task1.ui.AllService.hospital;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.example.task1.R;
import com.example.task1.application.MyApplication;
import com.example.task1.base.BaseActivity;
import com.example.task1.bean.HospitalUserManagerBean;
import com.example.task1.http.Api;
import com.example.task1.util.ApiResp;
import com.example.task1.util.CallBack;

import java.util.HashMap;

import retrofit2.Call;

public class HospitalUserModificationActivity extends BaseActivity {

    private int id;
    private EditText tvUserName;
    private EditText tvAddress;
    private EditText tvBirthday;
    private EditText tvCardId;
    private EditText tvSex;
    private EditText tvTel;
    private Button btnSave;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_hospital_user_modification);
        initView();
        business();
    }

    private void business() {
        btnSave.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String name = tvUserName.getText().toString();
                String birthday = tvBirthday.getText().toString();
                String address = tvAddress.getText().toString();
                String cardId = tvCardId.getText().toString();
                String sex = tvSex.getText().toString() == "男" ? "0" : "1";
                String tel = tvTel.getText().toString();
                if (TextUtils.isEmpty(name)) {
                    tvUserName.setError("不能为空");
                } else if (TextUtils.isEmpty(birthday)) {
                    tvBirthday.setError("不能为空");
                } else if (TextUtils.isEmpty(address)) {
                    tvAddress.setError("不能为空");
                } else if (TextUtils.isEmpty(cardId)) {
                    tvCardId.setError("不能为空");
                } else if (TextUtils.isEmpty(sex)) {
                    tvSex.setError("不能为空");
                } else if (TextUtils.isEmpty(tel)) {
                    tvTel.setError("不能为空");
                } else {
                    HashMap<String, Object> map = new HashMap<>();
                    map.put("name", name);
                    map.put("birthday", birthday);
                    map.put("address", address);
                    map.put("cardId", cardId);
                    map.put("sex", sex);
                    map.put("tel", tel);
                    if (id == 0) {
                        map.put("id", MyApplication.Hospital.userDate.getId());
                        Call<ApiResp> put = Api.api().put(MyApplication.TOKEN, "/prod-api/api/hospital/patient", Api.getBody(map));
                        Api.send(put, ApiResp.class, new CallBack<ApiResp>() {
                            @Override
                            public void success(ApiResp data) {
                                MyApplication.show("修改成功");
                                finish();
                            }

                            @Override
                            public void error(String msg) {

                            }
                        });
                    } else {
                        Call<ApiResp> put = Api.api().post(MyApplication.TOKEN, "/prod-api/api/hospital/patient", Api.getBody(map));
                        Api.send(put, ApiResp.class, new CallBack<ApiResp>() {
                            @Override
                            public void success(ApiResp data) {
                                MyApplication.show("新增成功");
                                finish();
                            }

                            @Override
                            public void error(String msg) {

                            }
                        });
                    }
                }
            }
        });
    }

    @Override
    public String getMyTitle() {
        id = getIntent().getIntExtra("id", 0);
        if (id == 0) {
            return "修改问诊卡片";
        } else {
            return "新增问诊卡片";
        }
    }

    private void initView() {
        tvUserName = (EditText) findViewById(R.id.tv_userName);
        tvAddress = (EditText) findViewById(R.id.tv_address);
        tvBirthday = (EditText) findViewById(R.id.tv_birthday);
        tvCardId = (EditText) findViewById(R.id.tv_cardId);
        tvSex = (EditText) findViewById(R.id.tv_sex);
        tvTel = (EditText) findViewById(R.id.tv_tel);
        if (id == 0) {
            HospitalUserManagerBean.RowsDTO userDate = MyApplication.Hospital.userDate;
            tvAddress.setText(userDate.getAddress());
            tvUserName.setText(userDate.getName());
            tvBirthday.setText(userDate.getBirthday());
            tvCardId.setText(userDate.getCardId());
            tvSex.setText(userDate.getAddress() == "0" ? "男" : "女");
            tvTel.setText(userDate.getTel());
        }
        btnSave = (Button) findViewById(R.id.btn_save);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        MyApplication.Hospital.userDate = null;
    }
}